<template>
  <div>
    <div class="add_cell">
      <div class="add_cell_left">
        <span>*</span>
        姓名
      </div>
      <div class="add_cell_right">
        <input type="text" placeholder="请输入就诊人姓名" v-model="name" />
      </div>
    </div>
    <div class="add_cell">
      <div class="add_cell_left">
        <span>*</span>
        身份证号
      </div>
      <div class="add_cell_right">
        <input type="text" placeholder="请输入身份证号" v-model="idNo" />
      </div>
    </div>
    <div class="add_cell">
      <div class="add_cell_left">
        <span>*</span>
        手机号
      </div>
      <div class="add_cell_right">
        <input type="text" placeholder="请输入手机号" v-model="mobile" />
      </div>
    </div>
    <div class="add_cell">
      <div class="add_cell_left">
        <span>*</span>
        短信验证码
      </div>
      <div class="add_cell_right">
        <input type="text" placeholder="请输入验证码" @input="changeauthCd" style="width: 100px" v-model="code" />
        <span @click="getCode" v-if="sending == false" class="code">获取验证码</span>
        <span v-else class="code1">重新发送({{ time }})</span>
      </div>
    </div>
    <div class="page_btn" @click="addPat">确定</div>
  </div>
</template>
<script>
import { sendPhoneCode, createPat, getWxSign } from "@/api/api";
export default {
  data() {
    return {
      hosId: "21010005",
      idType: "1",
      name: "",
      idNo: "",
      mobile: "",
      sex: "",
      birthday: "",
      code: "",
      sending: false,
      time: 60,
      appId: "",
      myreg: /^[1][3,4,5,6,7,8,9][0-9]{9}$/,
      reg: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
    };
  },
  created() {
    this.getWxSign()
  },
  methods: {
    addPat() {
      const that = this
      if (!that.name) {
        alert("姓名不能为空！");
        return;
      }
      // if (!that.reg.test(that.idNo)) {
      //   alert("身份证格式不对！");
      //   return;
      // }
      if (!that.myreg.test(that.mobile)) {
        alert("手机号格式不正确！");
        return;
      }
      if (!that.code) {
        alert("验证码不能为空！");
        return;
      }
      var data = {
        hosId: that.hosId,
        idType: that.idType,
        name: that.name,
        idNo: that.idNo,
        mobile: that.mobile,
        sex: parseInt(that.idNo.substr(16, 1)) % 2 == 1 ? "1" : "2",
        birthday:
          that.idNo.substring(6, 10) +
          "-" +
          that.idNo.substring(10, 12) +
          "-" +
          that.idNo.substring(12, 14),
        code: that.code,
        isDefault: "0",
      };

      if (that.getAge(that.idNo) > 16) {
        var faceQsdata = {
          'name': that.name.toString(),
          'id_card_number': that.idNo.toString(),
          'mobile': that.mobile.toString()

        }
        wx.invoke("requestWxFacePictureVerify", {
          "appId": that.appId,
          "request_verify_pre_info": JSON.stringify(faceQsdata)
        }, function (res) {
          if (res.err_code == 0) {
            var verify_identifier = res.verify_result;
            // 开发者选择是否使用后台文档获取本次认证的其他信息
            that.createPat(data)

          } else {
            var ret = res.err_msg;
            ret += " err_code: " + res.err_code;

            alert("人脸核身失败!");
          }
        });
      } else {
        that.createPat(data)
      }





      console.log(data);
    },
    createPat(data) {
      createPat(data).then(res => {
        if (res.code == 200) {
          alert("添加成功")
          this.$router.replace({
            path:"/my_patient"
          })
        }
      })
    },

    getWxSign() {

      getWxSign({
        url: window.location.href,
      }).then((res) => {
        this.appId = res.data.appId
        wx.config({
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印
          appId: res.data.appId, // 必填，公众号的唯一标识
          timestamp: res.data.timestamp, // 必填，生成签名的时间戳
          nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
          signature: res.data.signature, // 必填，签名
          jsApiList: ["requestWxFacePictureVerify"], // 必填，需要使用的JS接口列表

        });
        wx.ready(function () {
          console.log("ready");
          // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中
        });
        wx.error(function (err) {
          console.log("error:" + err);
          // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名
        });

      });
    },
    getCode() {
      if (!this.myreg.test(this.mobile)) {
        alert("手机号格式不正确！");
        return;
      }
      const data = {
        phone: this.mobile,
        type: "BIND",
      };
      sendPhoneCode(data).then((res) => {
        if (res.code == 200) {
          this.$vux.toast.show("发送成功");
          this.sending = true;
          this.countDown();
        }
      });
    },

    changeauthCd() {
      if (this.code.length > 6) {
        this.code = this.code.slice(0, 6)
      }
    },
    // 倒计时方法
    countDown() {
      if (this.time === 0) {
        this.sending = false;
        this.time = 60;
        return;
      } else {
        this.sending = true;
        this.time--;
      }
      setTimeout(() => {
        this.countDown();
      }, 1000);
    },
    getAge(identityCard) {
      var len = (identityCard + "").length;
      if (len == 0) {
        return 0;
      } else {
        if ((len != 15) && (len != 18))//身份证号码只能为15位或18位其它不合法
        {
          return 0;
        }
      }
      var strBirthday = "";
      if (len == 18)//处理18位的身份证号码从号码中得到生日和性别代码
      {
        strBirthday = identityCard.substr(6, 4) + "/" + identityCard.substr(10, 2) + "/" + identityCard.substr(12, 2);
      }
      if (len == 15) {
        strBirthday = "19" + identityCard.substr(6, 2) + "/" + identityCard.substr(8, 2) + "/" + identityCard.substr(10, 2);
      }
      //时间字符串里，必须是“/”
      var birthDate = new Date(strBirthday);
      var nowDateTime = new Date();
      var age = nowDateTime.getFullYear() - birthDate.getFullYear();
      //再考虑月、天的因素;.getMonth()获取的是从0开始的，这里进行比较，不需要加1
      if (nowDateTime.getMonth() < birthDate.getMonth() || (nowDateTime.getMonth() == birthDate.getMonth() && nowDateTime.getDate() < birthDate.getDate())) {
        age--;
      }
      return age;

    },
  },
};
</script>
<style lang="less">
.add_cell {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 16px;
  padding: 0.75rem 0;
  border-bottom: 1px solid #e7ecf5;

  &_left {
    span {
      color: red;
    }
  }

  &_right {
    input {
      border: none;
      text-align: right;
      font-size: 1rem;
      outline: none;
    }

    .code {
      border: 1px solid #448eff;
      border-radius: 3px;
      padding: 3px 10px;
      color: #448eff;
      margin-left: 10px;
      font-size: 15px;
    }

    .code1 {
      border: 1px solid #a4adbe;
      border-radius: 3px;
      padding: 3px 10px;
      color: #a4adbe;
      margin-left: 10px;
      font-size: 15px;
    }
  }
}
</style>
